<template>
  <div v-if="isAuth(['ROOT'])">
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
      <el-form-item prop="type">
        <el-input
            v-model="dataForm.type"
            placeholder="类型名称"
            size="medium"
            class="input"
            clearable="clearable"
        />
      </el-form-item>
      <el-form-item>
        <el-button size="medium" type="primary" @click="searchHandle()">查询</el-button>
        <el-button size="medium" type="primary" @click="addHandle()">新增</el-button>
        <el-button size="medium" type="danger" @click="deleteHandle()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        cell-style="padding: 4px 0"
        style="width: 100%;"
        size="medium"
    >
      <el-table-column
          type="selection"
          :selectable="selectable"
          header-align="center"
          align="center"
          width="50"
      />
      <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
        <template #default="scope">
          <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="罚款类型"/>
      <el-table-column header-align="center" align="center" label="罚款金额">
        <template #default="scope">
          <span>{{ scope.row.money }}元</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="系统自带">
        <template #default="scope">
          <span>{{ scope.row.systemic ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template #default="scope">
          <el-button type="text" size="medium" @click="updateHandle(scope.row.id)">修改</el-button>
          <el-button
              type="text"
              size="medium"
              :disabled="scope.row.canDelete == 'false' || scope.row.systemic == '是'"
              @click="deleteHandle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="loadDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './components/amect_type-add-or-update.vue';

export default {
  components: {
    AddOrUpdate
  },
  data: function () {
    return {
      dataForm: {
        type: null
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      dataRule: {
        type: [{required: false, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,10}$', message: '类型名称格式错误'}]
      }
    };
  },
  methods: {
    updateHandle(id) {
      this.addOrUpdateVisible = !this.addOrUpdateVisible
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      })
    },
    // pageSize改变时
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.loadDataList();
    },
    // currentPage改变时
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.loadDataList();
    },
    searchHandle() {
      let data = {
        type: this.dataForm.type,
        page: this.pageIndex,
        length: this.pageSize
      }
      console.log(data)
      this.$http('amect_type/searchAmectPageByType', 'POST', data, true, resp => {
        if (resp.code === 200) {
          let page = resp.page;
          this.dataList = page.list;
          this.totalCount = page.totalCount;
          this.dataListLoading = false;
        }
      })
    },
    // 加载表格数据
    loadDataList() {
      this.dataListLoading = true;
      let data = {
        type: this.dataForm.type,
        page: this.pageIndex,
        length: this.pageSize
      }
      this.$http('amect_type/searchAmectPageByType', 'POST', data, true, resp => {
        if (resp.code === 200) {
          let page = resp.page;
          this.dataList = page.list;
          this.totalCount = page.totalCount;
          this.dataListLoading = false;
        }
      })
    },
    addHandle() {
      this.addOrUpdateVisible = !this.addOrUpdateVisible
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init();
      })
    },
    deleteHandle(id) {
      let ids = id
          ? [id]
          : this.dataListSelections.map(item => {
            return item.id
          })
      if (ids.length === 0) {
        this.$message({
          message: "没有选中记录",
          type: "warning",
          duration: 1200
        })
      } else {
        this.$confirm(`确定要删除选中的记录？`, `提示`, {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.$http("amect_type/delete", "POST", {ids}, true, resp => {
            if (resp.code === 200 && resp.rows > 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500
              })
              this.loadDataList();
            } else {
              this.$message({
                message: "未能删除记录",
                type: "warning",
                duration: 1200
              })
            }
          })
        })
      }
    },
    selectionChangeHandle: function (val) {
      this.dataListSelections = val;
    },
  },
  created: function initData() {
    let data = {
      page: this.pageIndex,
      length: this.pageSize
    }
    console.log(data)
    this.$http('amect_type/searchAllType', 'POST', data, true, resp => {
      if (resp.code === 200) {
        let page = resp.page;
        this.dataList = page.list;
        this.totalCount = page.totalCount;
        this.dataListLoading = false;
      }
    })
  }
};
</script>

<style lang="less" scoped="scoped"></style>
